//
//  STLanguageHelper.m
//  
//
//  Created by jerry on 2019/5/2.
//  Copyright © 2019 jerry. All rights reserved.
//

#import "STLanguageHelper.h"

#define LANGUAGE_TABLE_NAME @"STLocalizable"

@interface STLanguageHelper()

@property (nonatomic, strong) NSDictionary *languageInfo;   //当前语言包类型

@end

@implementation STLanguageHelper

static STLanguageHelper *instance = nil;

+ (instancetype)sharedLanguageHelper {
    
    return [[self alloc] init];
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [super allocWithZone:zone];
    });
    return instance;
}

- (instancetype)init {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [super init];
        //初始化设置
        [self setupLanguageType];
    });
    return instance;
}

/**
 第一次设定默认语言包
 */
- (void)setupLanguageType {
    
    //先获取本地NSUserDefaults中是否有存储
    id languageInfo = [[NSUserDefaults standardUserDefaults] objectForKey:AppLanguage];
    
    if (languageInfo && [languageInfo isKindOfClass:[NSDictionary class]]) {
        _languageInfo = (NSDictionary *)languageInfo;
    }
    else {
        //NSUserDefaults中没有存储，获取系统 第一个语言
        NSArray  *languages = [NSLocale preferredLanguages];
        NSString *language = [languages objectAtIndex:0];   //获得首选语言版本
        
        NSDictionary *item = nil;
        if ([language hasPrefix:@"zh-Hans"]) {//开头匹配
            item = [NSDictionary dictionaryWithObjectsAndKeys: @"简体中文", LanguageName, language, LanguageType, nil];
            [[NSUserDefaults standardUserDefaults] setObject:item forKey:AppLanguage];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
        else if ([language hasPrefix:@"zh-Hant"]) { //其他的中文（繁体，台湾繁体，香港繁体，澳门繁体）
            item = [NSDictionary dictionaryWithObjectsAndKeys: @"繁体中文", LanguageName, language, LanguageType, nil];
            [[NSUserDefaults standardUserDefaults] setObject:item forKey:AppLanguage];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
        else if ([language hasPrefix:@"ms"]) { // 马来文
            item = [NSDictionary dictionaryWithObjectsAndKeys: @"Malay", LanguageName, language, LanguageType, nil];
            [[NSUserDefaults standardUserDefaults] setObject:item forKey:AppLanguage];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
        else {
            item = [NSDictionary dictionaryWithObjectsAndKeys: @"English", LanguageName, language, LanguageType, nil];
            [[NSUserDefaults standardUserDefaults] setObject:item forKey:AppLanguage];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
        _languageInfo = [NSDictionary dictionaryWithDictionary:item];
    }
}


/**
 多语言显示
 @param key <#key description#>
 @return 对应语言显示
 */
- (NSString *)languageDisplay:(NSString *)key {
    
    if (_languageInfo == nil) {
        [self setupLanguageType];
    }
    
    return [self finalLanguageDisplay:key];
}

- (NSString *)finalLanguageDisplay:(NSString *)key {
    
    NSString *language = _languageInfo[LanguageType];
    NSString *localizableStr = nil;
    
    if ([language hasPrefix:@"zh-Hans"]) {  //简体中文
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"zh-Hans" ofType:@"lproj"];
        localizableStr = [[NSBundle bundleWithPath:path] localizedStringForKey:key value:nil table:LANGUAGE_TABLE_NAME];
    }
    else if ([language hasPrefix:@"zh-Hant"]) { //其他的中文（繁体，台湾繁体，香港繁体，澳门繁体）
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"zh-Hant" ofType:@"lproj"];
        localizableStr = [[NSBundle bundleWithPath:path] localizedStringForKey:key value:nil table:LANGUAGE_TABLE_NAME];
    }
    else if ([language hasPrefix:@"ms"]) { //其他的中文（繁体，台湾繁体，香港繁体，澳门繁体）
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"ms" ofType:@"lproj"];
        localizableStr = [[NSBundle bundleWithPath:path] localizedStringForKey:key value:nil table:LANGUAGE_TABLE_NAME];
    }
    else {        //其他的直接设为英语
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
        localizableStr = [[NSBundle bundleWithPath:path] localizedStringForKey:key value:nil table:LANGUAGE_TABLE_NAME];
    }
    
    return localizableStr;
}

/**
 修改本地语言包
 @param lageType 语言包类型
 */
- (void)changeLocalLanguage:(STLageType)lageType {
    
    NSDictionary *item = nil;
    
    switch (lageType) {
        case STLT_zh_Hans: {
            item = [NSDictionary dictionaryWithObjectsAndKeys: @"简体中文", LanguageName, @"zh-Hans", LanguageType, nil];
            [[NSUserDefaults standardUserDefaults] setObject:item forKey:AppLanguage];
            [[NSUserDefaults standardUserDefaults] synchronize];
            break;
        }
        case STLT_zh_Hant: {
            item = [NSDictionary dictionaryWithObjectsAndKeys: @"繁体中文", LanguageName, @"zh-Hant", LanguageType, nil];
            [[NSUserDefaults standardUserDefaults] setObject:item forKey:AppLanguage];
            [[NSUserDefaults standardUserDefaults] synchronize];
            break;
        }
        case STLT_en: {
            item = [NSDictionary dictionaryWithObjectsAndKeys: @"English", LanguageName, @"en", LanguageType, nil];
            [[NSUserDefaults standardUserDefaults] setObject:item forKey:AppLanguage];
            [[NSUserDefaults standardUserDefaults] synchronize];
            break;
        }
        case STLT_mala: {
            item = [NSDictionary dictionaryWithObjectsAndKeys: @"Malay", LanguageName, @"ms", LanguageType, nil];
            [[NSUserDefaults standardUserDefaults] setObject:item forKey:AppLanguage];
            [[NSUserDefaults standardUserDefaults] synchronize];
            break;
        }
        case STLT_unknown: {
            break;
        }
        default:
            break;
    }
    if (item != nil) {
        _languageInfo = item;
    }
}

/**
 当前设置的语言
 @return 当前语言枚举
 */
- (STLageType)languageOptions {
    
    if (_languageInfo == nil) {
        [self setupLanguageType];
    }
    
    NSString *language = _languageInfo[LanguageType];
    
    if ([language hasPrefix:@"zh-Hans"]) {  //简体中文
        return STLT_zh_Hans;
    }
    else if ([language hasPrefix:@"zh-Hant"]) { //其他的中文（繁体，台湾繁体，香港繁体，澳门繁体）
        return STLT_zh_Hant;
    }
    else if ([language hasPrefix:@"ms"]) {      //马来西亚
        return STLT_mala;
    }
    else if ([language hasPrefix:@"en"]) {      //其他的直接设为英语
        return STLT_en;
    }
    else {
        return STLT_unknown;
    }
}

@end
